package com.code_g.dao.Impl;

import com.code_g.dao.UserDao;
import com.code_g.domain.User;
import com.code_g.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<User> findAll() {
        String sql = "select * from user";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public User login(User user) {
        try{
            String sql = "select * from user where username = ? and password = ?";
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),user.getUsername(),user.getPassword());
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void addUser(User user) {
        String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
        jdbcTemplate.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail());
    }

    @Override
    public void deleteUser(int id) {
        String sql = "delete from user where id = ?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public User findUser(int id) {
        String sql = "select * from user where id = ?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),id);
    }

    @Override
    public void updateUser(User user) {
        String sql = "update user set name = ?,gender = ?,age = ?,address = ?,qq = ?,email = ? where id = ?";
        jdbcTemplate.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail(),user.getId());
    }

    @Override
    public void delSelect(String[] ids) {
        if (ids.length != 0){
            for (String id : ids) {
                deleteUser(Integer.parseInt(id));
            }
        }
    }

    @Override
    public int findTotalCount(Map<String, String[]> map) {
        String sql = "select count(id) from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        ArrayList<Object> list = new ArrayList<Object>();
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            if ("currentPage".equals(s) || "rows".equals(s)){
                continue;
            }
            if (s != null){
                String[] strings = map.get(s);
                list.add("%"+strings[0]+"%");
                sb.append(" and "+ s +" like ? ");
            }
        }
        return jdbcTemplate.queryForObject(sb.toString(),Integer.class,list.toArray());
    }

    @Override
    public List<User> findByPage(int start, int rows, Map<String, String[]> map) {
        String sql = "select * from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        ArrayList<Object> list = new ArrayList<Object>();
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            if ("currentPage".equals(s) || "rows".equals(s)){
                continue;
            }
            if (s != null){
                String[] strings = map.get(s);
                list.add("%"+strings[0]+"%");
                sb.append(" and "+ s +" like ? ");
            }
        }
        sb.append(" limit ?,? ");
        list.add(start);
        list.add(rows);
        return jdbcTemplate.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),list.toArray());
    }
}
